package com.cplatform.movie.back.order.web;

import java.sql.SQLException;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cplatform.dbhelp.page.Page;
import com.cplatform.movie.back.order.dao.OrderItemLinkDao;
import com.cplatform.movie.back.order.entity.Logistics;
import com.cplatform.movie.back.order.entity.OrderItemLink;
import com.cplatform.movie.back.order.service.LogisticsService;
import com.cplatform.movie.back.order.service.OrderItemLinkService;
import com.cplatform.movie.back.order.service.OrderService;

/**
 * Title. 订单控制层<br>
 * Description.
 * <p>
 * Copyright: Copyright (c) 2015年6月29日 上午10:03:36
 * <p>
 * Author: liuyt
 * <p>
 * Version: 1.0
 * <p>
 */
@Controller
@RequestMapping("/order")
public class OrderController {
	private Logger log = Logger.getLogger(OrderController.class);

	@Autowired
	private OrderService orderService;
	@Autowired
	private OrderItemLinkService orderItemLinkService;
	@Autowired
	private OrderItemLinkDao orderItemLinkDao;
	@Autowired
	private LogisticsService logisticsService;

	/**
	 * @Title: 进入订单列表页面 <br>
	 * @param model
	 * @return
	 * @throws
	 */
	@RequestMapping("/list")
	public String list(HttpSession session, OrderItemLink order, @RequestParam(required = false, value = "page", defaultValue = "1") int page, Model model) {
		Page<OrderItemLink> pageData = null;
		try {
			pageData = orderItemLinkService.list(order, page, session);
		} catch (SQLException e) {
			log.error("订单分页数据查询失败,msg:" + e.getMessage());
		}

		model.addAttribute("order", order);
		model.addAttribute("pageData", pageData);
		return "order/order-list";
	}

	@RequestMapping("/view/{id}")
	public String view(Model model, @PathVariable Long id) {
		OrderItemLink itemLink = null;
		Logistics logistics = null;
		try {
			itemLink = orderItemLinkDao.findOne(id);
			logistics = logisticsService.findByOrderId(itemLink.getOrderId());
		} catch (SQLException e) {
			log.error("订单详情查询失败,msg:" + e.getMessage());
		}
		model.addAttribute("order", itemLink);
		model.addAttribute("logistics", logistics);
		return "order/order-view";
	}

}
